// order//order/giveFriend/index.js
import getUserCard from "../../../utils/getUserCard.js";
const utils = require('../../../utils/util.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    slider: '',
    company: '',
    orderInfo: {},
    proInfo: {},
    codeInfo: {},
    screenHeight: 500
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var screenHeight = wx.getSystemInfoSync().screenHeight;
    this.getInfo(options.createuid);
    this.getCode(options.oid);
    this.getOrderDetail(options.oid,options.uid);
    this.setData({
      screenHeight: screenHeight-80,
      oid: options.oid,
      createuid: options.createuid
    })
  },

  // 订单详情
  getOrderDetail: function(oid, uid){
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Order/GetOrder?uid=' + uid + '&oid=' + oid,
      success: function(res){
        if(res.data.Data){
          let orderinfo = res.data.Data.OrderInfo;
          let proInfo = res.data.Data.OrderProductList[0].Info;
          orderinfo.expireTime = '';
          if(res.data.Data.OrderProductList[0].Extend && res.data.Data.OrderProductList[0].Extend.UseEndTime){
            orderinfo.expireTime = res.data.Data.OrderProductList[0].Extend.UseEndTime.substr(0,10);
          }
          if(proInfo.ShowImg.indexOf('ps') != -1){
            proInfo.ShowImg = utils.formatAvaterByProHead(proInfo.ShowImg);
          }else{
            proInfo.ShowImg = utils.formatAvater(proInfo.ShowImg,'https://static.711688.net.cn/upload/store/'+ app.globalData.storeId +'/product/editor/');
          }
          that.setData({
            orderInfo: orderinfo,
            proInfo: proInfo
          })
        }
      }
    })
  },

  //获取商家信息
  getInfo: function(createUid) {
    var that = this;
    getUserCard(createUid, function (cardid) {
      wx.request({
        url: app.globalData.host + "/api/Staff/GetInfo?id=" + cardid,
        success: function(res) {
          if (res.data.Code == 0) {
            let slider = '';
            if(res.data.Data.IndustryCode > 0){//商家
              let sliders = res.data.Data.Slides;
              sliders = sliders.split(",");
              slider = sliders[sliders.length-1];
              slider = utils.formatAvater(slider, 'https://static.711688.net.cn/upload/card/source/');
            }else{//个人版
              slider = res.data.Data.Avater;
              slider = utils.formatAvater(slider, 'https://static.711688.net.cn/upload/card/source/');
            }
            that.setData({
              slider: slider,
              company: res.data.Data.Company
            })
          }
        },
      })
    })
  },

  getCode: function(oid){
    let that = this;
    wx.request({
      url: app.globalData.host+ '/api/order/GetOrderCheck?oid=' + oid,
      success: function(res){
        if(res.data.Code == 0){
          if(res.data.Data){
            let orderInfo = res.data.Data;
            orderInfo.codesnList = [];
            let codelist = orderInfo.CodeSN.split(",");
            let lengthIndex = codelist.length;
            for(let m = lengthIndex-1; m >= 0; m--){
              orderInfo.codesnList.push(codelist[m]);
            }
            if(orderInfo.codesnList.length>0){
              let list = orderInfo.codesnList;
              for(let i = 0; i < list.length; i++){
                list[i] = list[i].replace(/\s/g,'').replace(/(.{4})/g,"$1 ");
              }
              orderInfo.canUse = list.length - orderInfo.CheckCount;
            }
            that.setData({
              codeInfo: orderInfo
            })
          }
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // 分享
  onShareAppMessage: function () {
    var that = this;
    return {
      path: '/order/order/receiveOrder/index?fromuid=' + app.globalData.userInfo.Uid + '&oid=' + that.data.oid + '&createuid=' + that.data.createuid,
      success: function (res) {
        wx.showModal({
          title: '提示',
          content: '转发成功',
          showCancel: false
        })
      }
    }
  }
})